import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;

public class Test {
    //
}

class Solution
{
    public int minMutation(String startGene, String endGene, String[] bank)
    {
        Set<String> vis = new HashSet<>(); // ⽤来标记已经搜索过的状态
        Set<String> hash = new HashSet<>(); // ⽤来统计基因库⾥⾯的字符串
        for(String s : bank) hash.add(s);
        char[] change = {'A', 'C', 'G', 'T'};
        if(startGene.equals(endGene)) return 0;
        if(!hash.contains(endGene)) return -1;
        Queue<String> q = new LinkedList<>();
        q.add(startGene);
        vis.add(startGene);
        int step = 0;
        while(!q.isEmpty())
        {
            step++;
            int sz = q.size();
            while(sz-- != 0)
            {
               String t = q.poll();
                for(int i = 0; i < 8; i++)
                {
                    char[] tmp = t.toCharArray();
                    for(int j = 0; j < 4; j++)
                    {
                        tmp[i] = change[j];
                        String next = new String(tmp);
                        if(hash.contains(next) && !vis.contains(next))
                        {
                            if(next.equals(endGene)) return step;
                            q.add(next);
                            vis.add(next);
                        }
                    }
                }
            }
        }
        return -1;
    }
}
